﻿using FinalWork.Model.EF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalWork
{
   


    public partial class FormAdd : Form
    {

        
        
            // 定义委托
            public delegate void DataTransferEventHandler(DataRow dataRow);
            // 定义事件
            public event DataTransferEventHandler DataTransfer;
            // 假设manuAdd的数据存储在DataTable中，这里获取DataRow
            private DataRow GetDataRowFromManuAdd()
            {
                DataTable manuAddDataTable = // 获取manuAdd对应的DataTable
                                             // 假设你已经有获取DataTable的方法，这里填充获取逻辑
                    null;
                if (manuAddDataTable.Rows.Count > 0)
                {
                    return manuAddDataTable.Rows[0];
                }
                return null;
            }
           
        



        private manuServices01 service;


       
        public FormAdd()
        {
            InitializeComponent();
            service = new manuServices01();
           

        }

        private void FormAdd_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var lst = service.GetAll01();
            this.dataGridView1.DataSource = lst;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //新增
            FormAdd01 formAdd01 = new FormAdd01();
            var result = formAdd01.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAll01();
                this.dataGridView1.DataSource = lst;
            }

           

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataRow dataRow = GetDataRowFromManuAdd();
            if (dataRow != null)
            {
                DataTransfer?.Invoke(dataRow);
            }
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            // 获取当前行的id
            if (this.dataGridView1.CurrentRow != null)
            {
                Guid id = (Guid)this.dataGridView1.CurrentRow.Cells["Id"].Value;
                string name = (string)this.dataGridView1.CurrentRow.Cells["菜名"].Value;
                var result = MessageBox.Show($"是否删除【{name}】的信息?",
                                           "系统提示",
                                           MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    //用户确认
                    service.Delete(id);
                    var lst = service.GetAll01();
                    this.dataGridView1.DataSource = lst;
                }
            }
        }
    }
}
